package org.khd.org.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.khd.base.constants.BaseConstants;
import org.khd.base.service.impl.BaseServiceImpl;
import org.khd.org.domain.Employee;
import org.khd.org.domain.Shop;
import org.khd.org.domain.ShopEmployee;
import org.khd.org.domain.ShopOperateLog;
import org.khd.org.dto.ShopAdminDto;
import org.khd.org.dto.ShopRegisterDto;
import org.khd.org.mapper.EmployeeMapper;
import org.khd.org.mapper.ShopEmployeeMapper;
import org.khd.org.mapper.ShopMapper;
import org.khd.org.mapper.ShopOperateLogMapper;
import org.khd.org.service.IShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author saber
 * @since 2024-04-27
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopEmployeeMapper shopEmployeeMapper;

    @Autowired
    private ShopOperateLogMapper shopOperateLogMapper;

    @Override
    @Transactional
    public void settlement(ShopRegisterDto shopRegisterDto) {
        //1.接收前端传来的参数，并对参数进行非空验证
        ShopAdminDto shopAdminDto = shopRegisterDto.getShopAdminDto();
        validateParam(shopRegisterDto, shopAdminDto);

        // 若店铺名不存在，添加进数据库
        Shop shop = new Shop();
        //源目标相同字段名复制给目标对象相同字段
        BeanUtils.copyProperties(shopRegisterDto, shop);
        shop.setRegisterTime(new Date());
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUDIT);
        shopMapper.add(shop);

        //3.前端传来的管理员信息中的电话或邮箱与数据库中的电话和邮箱比对，
        Employee employeeAdmin = employeeMapper.getByPhoneOrEmail(shopAdminDto.getPhone(),shopAdminDto.getEmail());

        ShopEmployee shopEmployee = new ShopEmployee();
        shopEmployee.setShopId(shop.getId());
        shopEmployee.setIsAdmin(1);
        if (Objects.isNull(employeeAdmin)){
            // 若电话和邮箱都不一样，则新增管理员信息和店铺信息，并将店铺ID和管理员ID关联
            employeeAdmin = new Employee();
            BeanUtils.copyProperties(shopAdminDto, employeeAdmin);
            employeeAdmin.setState(BaseConstants.Employee.STATE_WAIT_AUDIT);
            employeeMapper.insertEmp(employeeAdmin);
        }
        // 若有其中一个一样，则不新增用户，新增店铺信息后，然后将店铺ID和管理员ID关联；
        shopEmployee.setEmployeeId(employeeAdmin.getId());
        shopEmployeeMapper.add(shopEmployee);

        //4.保存店铺操作日志到数据库
        ShopOperateLog shopOperateLog = new ShopOperateLog();
        shopOperateLog.setShopId(shop.getId());
        shopOperateLog.setOperateType(1);
        shopOperateLog.setOperateId(null);
        shopOperateLog.setOperateTime(new Date());
        shopOperateLog.setNote("店铺入驻审核");
        shopOperateLogMapper.add(shopOperateLog);

        //5.比对两次密码，若密码不一致，则返回让用户重新输入密码
        if (!shopAdminDto.getPassword().equals(shopAdminDto.getConfirmPassword())){
            throw new RuntimeException("两次密码不一致，请重新输入！");
        }

        //6.对店铺入驻信息做自动审核

        //7.若审核通过，发送激活邮件；
        // 若审核未通过，发送邮件告知审核未通过原因
    }

    //抽取方法快捷键Ctrl + alt + m
    private void validateParam(ShopRegisterDto shopRegisterDto, ShopAdminDto shopAdminDto) {
        if (Objects.isNull(shopAdminDto) || StringUtils.isBlank(shopRegisterDto.getName())
        ||  StringUtils.isBlank(shopRegisterDto.getTel()) || StringUtils.isBlank(shopRegisterDto.getAddress())
        ||  StringUtils.isBlank(shopAdminDto.getUsername()) || StringUtils.isBlank(shopAdminDto.getEmail())
        ||  StringUtils.isBlank(shopAdminDto.getPhone()) || StringUtils.isBlank(shopAdminDto.getPassword())
        ||  StringUtils.isBlank(shopAdminDto.getConfirmPassword())
        ){
            throw new RuntimeException("店铺入驻信息不完整，请检查后重新提交！");
        }
        //2.前端传来的店铺名与数据库的店铺名比对，
        Shop shop = shopMapper.getByName(shopRegisterDto.getName());
        // 若存在，则返回信息，让重新修改店铺名；
        if (Objects.nonNull(shop)){
            throw new RuntimeException("店铺名已存在，请重新输入！");
        }
    }
}
